package com.duguo.dynamicmvc.view.dynamic.jsp;

import java.io.File;

import javax.servlet.ServletContext;

import org.springframework.web.context.ServletContextAware;

import com.duguo.dynamicmvc.view.View;
import com.duguo.dynamicmvc.view.resolver.ViewResolver;

public class JspViewResolver implements ViewResolver,ServletContextAware {

    private String _viewPrefix = "/WEB-INF/jsp";
    
    private String _viewSurfix = ".jsp";
    
	private ServletContext _servletContext;

    public View resolve(String viewName) {
        String fullName = _viewPrefix + viewName + _viewSurfix;
        File viewFile = new File(_servletContext.getRealPath(fullName));
        if (viewFile.exists()) {
            return new JspView(fullName);
        }
        return null;
    }

    public void setViewPrefix(String viewPrefix) {
        this._viewPrefix = viewPrefix;
    }

    public void setViewSurfix(String viewSurfix) {
        this._viewSurfix = viewSurfix;
    }

	public void setServletContext(ServletContext servletContext) {
		_servletContext = servletContext;
	}

}
